//index.js
var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    saveHidden: true,
    allSelect: true,
    noSelect: false,
    mallName: '',
    cartInfo: {},
    stockNumber: 0,
    delBtnWidth: 120 ,   //删除按钮宽度单位（rpx）
    storeid: app.globalData.storeId,
    isAccredit: false
  },
  empty: function (e) { },
  //输入框
  // blurNumber: function (e) {
  //   if (e.detail.value < 0) {
  //     return
  //   } else {
  //     var that = this;
  //     var index = e.currentTarget.dataset.index;
  //     var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
  //     var cart = this.data.cartInfo;
  //     var list = cart.StoreCartList[0].CartProductList;
  //     if (t == 1) {
  //       var pindex = e.currentTarget.dataset.pindex;
  //       list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
  //     } else if (t == 2) {
  //       var pindex = e.currentTarget.dataset.pindex;
  //       list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
  //     }
  //     if (index !== "" && index != null) {
  //       var selList = that.getSelList(cart);
  //       console.log(e.detail.value);
  //       wx.request({
  //         url: app.globalData.host + '/api/cart/ChangeProductCount',
  //         data: {
  //           pid: list[parseInt(index)].OrderProductInfo.Pid,
  //           buyCount: e.detail.value,
  //           uid: app.globalData.userInfo.Uid,
  //           selList: selList
  //         },
  //         success: function (res) {
  //           if (res.data.Code == 0) {
  //             var cart = that.processImageHost(res.data.Data);
  //             that.setData({ cartInfo: cart });
  //             that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
  //           } else {
  //             wx.showToast({
  //               title: '系统错误',
  //               content: res.data.Message
  //             })
  //           }
  //         }
  //       })
  //     }
  //   }
  // },
  //输入框
  blurNumber: function (e) {
    var that = this;
    console.log("输入框");
    var input = e.detail.value;
    var pid = e.currentTarget.dataset.pid;
    if (input < 0) {
      return;
    } else {
      var index = e.currentTarget.dataset.index;
      var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
      var cart = that.data.cartInfo;
      var list = cart.StoreCartList[0].CartProductList;
      if (t == 1) {
        var pindex = e.currentTarget.dataset.pindex;
        list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
      } else if (t == 2) {
        var pindex = e.currentTarget.dataset.pindex;
        list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
      }
      if (index !== "" && index != null) {
        var selList = that.getSelList(cart);
        that.setData({
          buyCount: e.detail.value
        });
        wx.request({
          url: app.globalData.host + '/api/store/Product',
          data: {
            pid: pid
          },
          success: function (res) {
            that.setData({
              stockNumber: res.data.Data.StockNumber
            });
            if (that.data.stockNumber < input) {
              wx.showModal({
                title: '提示',
                content: '商品库存不足',
                showCancel: false
              });
              return;
            } else {
              wx.request({
                url: app.globalData.host + '/api/cart/ChangeProductCount',
                data: {
                  pid: list[parseInt(index)].OrderProductInfo.Pid,
                  buyCount: that.data.buyCount,
                  uid: app.globalData.userInfo.Uid,
                  selList: selList
                },
                success: function (res) {
                  if (res.data.Code == 0) {
                    var cart = that.processImageHost(res.data.Data);
                    that.setData({ cartInfo: cart });
                    that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
                  } else {
                    wx.showToast({
                      title: '系统错误',
                      content: res.data.Message
                    })
                  }
                }
              })
            }
          }
        })
      }
    }
  },

  //跳转选择赠品页面
  toSelectGift:function(e){
   var  cart = this.data.cartInfo;
   var  selList = this.getSelList(cart);
    console.log(e)
    wx.navigateTo({
      url: '/pages/gift-select/index?pmid=' + e.currentTarget.dataset.pmid + '&selList=' + selList,
    })
  },
  // 删除购物车中满赠商品POST /api/Cart/DelFullSend
  delectFullSend: function (e) {
    var cart = this.data.cartInfo;
    var selList = this.getSelList(cart);
    var pmid = e.currentTarget.dataset.pmid;
    var that = this;
    console.log(that.data.pmId)
    wx.request({
      url: app.globalData.host + '/api/Cart/DelFullSend',
      data: {
        pmId: pmid,
        uid: app.globalData.userInfo.Uid,
        selList: selList
      },
      success: function (res) {
        console.log(res);
        if (res.data.Code == 0) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              that.onShow();
            }
          })
        }
      }
    })
  },
  //获取元素自适应后的实际宽度
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);  //以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  onLoad: function () {
    let that = this;
    let isAccredit = util.isAccredit(function (isAccredit) {
      console.log(isAccredit);
      that.setData({
        isAccredit: isAccredit
      })
    });
    that.initEleWidth();
  },
  onShow: function () {
    var that = this;
    let back = '/pages/shop-cart/index';
    setTimeout(function () {
      if (!that.data.isAccredit) {
        console.log(that.data.isAccredit);
        wx.showModal({
          title: '提示',
          content: '登陆后即可查看购物车，是否确认登陆？',
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '../accredit/index?back=' + back,
              })
            }
          }
        })
        return;
      }
    }, 1000);
    wx.showLoading({
      title: '加载中',
    })
    // 获取购物车数据
    wx.request({
      url: app.globalData.host + '/api/cart/index',
      data: {
        uid: app.globalData.userInfo.Uid
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          var cart = that.processImageHost(res.data.Data);
          // console.log(cart)
          that.setData({
            saveHidden: true, allSelect: true, noSelect: false, mallName: wx.getStorageSync('mallName'), cartInfo: cart
          });
        }
      }
    })
  },
  toIndexPage: function () {
    wx.reLaunch({
      url: "/pages/index/index"
    });
  },

  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品

    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var left = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，container位置不变
        left = "margin-left:0px";
      } else if (disX > 0) {//移动距离大于0，container left值等于手指移动距离
        left = "margin-left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          left = "left:-" + delBtnWidth + "px";
        }
      }
      var list = this.data.cartInfo;
      // console.log(list)
      if (index != "" && index != null) {
        if(t==0){
          list.StoreCartList[0].CartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        }else if(t==1){
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        }else if (t == 2) {
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        } 
        else if (t == 3) {
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartSuitList[parseInt(pindex)].CartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        }
        

      }
    }
  },

  touchE: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品

    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var left = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
      var list = this.data.cartInfo;
      if (index !== "" && index != null) {
        if (t == 0) {
          list.StoreCartList[0].CartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        } else if (t == 1) {
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        } else if (t == 2) {
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        } 
         else if (t == 3) {
          var pindex = e.currentTarget.dataset.pindex;
          list.StoreCartList[0].CartSuitList[parseInt(pindex)].CartProductList[parseInt(index)].left = left;
          that.setData({ cartInfo: list });
        }
      }
    }
  },
  delItem: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cart = this.data.cartInfo;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品,3:组合
    var list = cart.StoreCartList[0].CartProductList;
    if (t == 1) {
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
    } else if (t == 2) {
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
    } 
    else if(t==3){
      var pmid = e.currentTarget.dataset.pmid;
      var selList = that.getSelList(cart);
      wx.request({
        url: app.globalData.host +'/api/cart/DelSuit',
        data:{
          pmid:pmid,
          uid:app.globalData.userInfo.Uid,
          selList:selList
        },
        success:function(res){
          if (res.data.Code == 0) {
            var cart = that.processImageHost(res.data.Data);
            that.setData({ cartInfo: cart });
            that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
          } else {
            wx.showToast({
              title: '系统错误',
              content: res.data.Message
            })
          }
        }
      })
      return;
    }
    var goods = list[parseInt(index)].OrderProductInfo;
    list[parseInt(index)].IsSelected = false;
    var selList = that.getSelList(cart);
    wx.request({
      url: app.globalData.host + '/api/cart/DelProduct',
      data: {
        pid: goods.Pid,
        uid: app.globalData.userInfo.Uid,
        selList: selList
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var cart = that.processImageHost(res.data.Data);
          that.setData({ cartInfo: cart });
          that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
        } else {
          wx.showToast({
            title: '系统错误',
            content: res.data.Message
          })
        }
      }
    })

  },
  selectTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cart = this.data.cartInfo;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
    var list = cart.StoreCartList[0].CartProductList;
    if(t==1){
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
    }
    else if (t == 2) {
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
    }
    if (index !== "" && index != null) {
      list[parseInt(index)].IsSelected = !list[parseInt(index)].IsSelected;
      var selList = that.getSelList(cart);
      console.log(selList);
      wx.request({
        url: app.globalData.host + '/api/cart/CancelOrSelectCartItem',
        data: {
          uid: app.globalData.userInfo.Uid,
          selList: selList
        },
        success: function (res) {
          console.log(res);
          if (res.data.Code == 0) {
            var cart = that.processImageHost(res.data.Data);
            that.setData({ cartInfo: cart });
            that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
          } else {
            wx.showToast({
              title: '系统错误',
              content: res.data.Message
            })
          }
        }
      })
    }
  },

  allSelect: function () {
    if (this.data.cartInfo.StoreCartList.length == 0) {
      return false;
    }
    var list = this.data.cartInfo.StoreCartList[0].CartProductList;
    var cutList = this.data.cartInfo.StoreCartList[0].CartFullCutList;
    var suitList = this.data.cartInfo.StoreCartList[0].CartSuitList;
    var allSelect = false;
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      if (curItem.IsSelected) {
        allSelect = true;
      } else {
        allSelect = false;
        break;
      }
    }
    for(var i=0;i<cutList.length;i++){
      for (var j = 0; j < cutList[i].FullCutCartProductList.length;j++){
        var curItem = cutList[i].FullCutCartProductList[j];
        if (curItem.IsSelected) {
          allSelect = true;
        } else {
          allSelect = false;
          break;
        }
      }
    }
    for (var i = 0; i < suitList.length; i++) {
      var curItem = suitList[i];
      if (curItem.IsSelected) {
        allSelect = true;
      } else {
        allSelect = false;
        break;
      }
    }
    return allSelect;
  },
  noSelect: function () {
    if (this.data.cartInfo.StoreCartList.length == 0) {
      return false;
    }
    var list = this.data.cartInfo.StoreCartList[0].CartProductList;
    var cutList = this.data.cartInfo.StoreCartList[0].CartFullCutList;
    var suitList = this.data.cartInfo.StoreCartList[0].CartSuitList;
    var SendList = this.data.cartInfo.StoreCartList[0].CartFullSendList;
    var noSelect = 0;
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      if (!curItem.IsSelected) {
        noSelect++;
      }
    }
    var c=0;
    for (var i = 0; i < cutList.length; i++) {
      for (var j = 0; j < cutList[i].FullCutCartProductList.length; j++) {
        c++;
        var curItem = cutList[i].FullCutCartProductList[j];
        if (!curItem.IsSelected) {
          noSelect++;
        } 
      }
    }
    for (var i = 0; i < SendList.length; i++) {
      for (var j = 0; j < SendList[i].FullSendMainCartProductList.length; j++) {
        c++;
        var curItem = SendList[i].FullSendMainCartProductList[j];
        if (!curItem.IsSelected) {
          noSelect++;
        }
      }
    }
    for (var i = 0; i < suitList.length; i++) {
      for (var j = 0; j < suitList[i].CartProductList.length; j++) {
        c++;
        var curItem = suitList[i].CartProductList[j];
        if (!curItem.IsSelected) {
          noSelect++;
        } 
      }
    }
    if (noSelect == (list.length+c)) {
      return true;
    } else {
      return false;
    }
  },
  setGoodsList: function (saveHidden, allSelect, noSelect) {
    this.setData({
      saveHidden: saveHidden,
      allSelect: allSelect,
      noSelect: noSelect
    });

  },
  bindAllSelect: function () {
    var that = this;
    var cart = this.data.cartInfo;
    var currentAllSelect = this.data.allSelect;
    var list = cart.StoreCartList[0].CartProductList;
    var cutList = cart.StoreCartList[0].CartFullCutList;
    var suitList = this.data.cartInfo.StoreCartList[0].CartSuitList;
    console.log('allselelct:' + currentAllSelect);
    if (currentAllSelect) {
      for (var i = 0; i < list.length; i++) {
        var curItem = list[i];
        curItem.IsSelected = false;
      }
      for (var i = 0; i < cutList.length; i++) {
        for (var j = 0; j < cutList[i].FullCutCartProductList.length; j++) {
          var curItem = cutList[i].FullCutCartProductList[j];
          curItem.IsSelected = false;
        }
      }
      for (var i = 0; i < suitList.length; i++) {
        suitList[i].IsSelected = false;
        for (var j = 0; j < suitList[i].CartProductList.length; j++) {
          var curItem = suitList[i].CartProductList[j];
          curItem.IsSelected = false;
        }
      }
    } else {
      for (var i = 0; i < list.length; i++) {
        var curItem = list[i];
        curItem.IsSelected = true;
      }
      for (var i = 0; i < cutList.length; i++) {
        for (var j = 0; j < cutList[i].FullCutCartProductList.length; j++) {
          var curItem = cutList[i].FullCutCartProductList[j];
          curItem.IsSelected = true;
        }
      }
      for (var i = 0; i < suitList.length; i++) {
        suitList[i].IsSelected=true;
        for (var j = 0; j < suitList[i].CartProductList.length; j++) {
          var curItem = suitList[i].CartProductList[j];
          curItem.IsSelected = true;
        }
      }
    }

    var selList = that.getSelList(cart);
    console.log(selList);
    wx.request({
      url: app.globalData.host + '/api/cart/CancelOrSelectCartItem',
      data: {
        uid: app.globalData.userInfo.Uid,
        selList: selList
      },
      success: function (res) {
        console.log(res);
        if (res.data.Code == 0) {
          var cart = that.processImageHost(res.data.Data);

          that.setData({ cartInfo: cart });
          that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
        } else {
          wx.showToast({
            title: '系统错误',
            content: res.data.Message
          })
        }
      }
    })

  },
  // jiaBtnTap: function (e) {
  //   var that=this;
  //   var index = e.currentTarget.dataset.index;
  //   var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
  //   var cart = this.data.cartInfo;
  //   console.log(cart)
  //   console.log(t)
  //   var list = cart.StoreCartList[0].CartProductList;
  //   if (t == 1) {
  //     var pindex = e.currentTarget.dataset.pindex;
  //     list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
  //   } else if (t == 2) {
  //     var pindex = e.currentTarget.dataset.pindex;
  //     list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
  //   } 
  //   if (index !== "" && index != null) {
  //     if (list[parseInt(index)].OrderProductInfo.BuyCount > 9) return;
  //     var selList = that.getSelList(cart);
  //     var buyCount = list[parseInt(index)].OrderProductInfo.BuyCount + 1;
  //     console.log(buyCount);
  //     wx.request({
  //       url: app.globalData.host + '/api/cart/ChangeProductCount',
  //       data: {
  //         pid: list[parseInt(index)].OrderProductInfo.Pid,
  //         buyCount: buyCount,
  //         uid: app.globalData.userInfo.Uid,
  //         selList: selList
  //       },
  //       success: function (res) {
  //         console.log(res);
  //         if (res.data.Code == 0) {
  //           var cart = that.processImageHost(res.data.Data);
  //           that.setData({ cartInfo: cart });
  //           that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
  //         } else {
  //           wx.showToast({
  //             title: '系统错误',
  //             content: res.data.Message
  //           })
  //         }
  //       }
  //     })
  //   }
  // },

  jiaBtnTap: function (e) {
    console.log("jiaBtnTap");
    console.log(e);
    console.log("jiaBtnTap");
    var that = this;
    var index = e.currentTarget.dataset.index;
    var pid = e.currentTarget.dataset.pid;
    var realCount = e.currentTarget.dataset.realcount;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
    var cart = this.data.cartInfo;
    var list = cart.StoreCartList[0].CartProductList;
    console.log(cart)
    console.log(t)
    wx.request({
      url: app.globalData.host + '/api/store/Product',
      data: {
        pid: pid
      },
      success: function (res) {
        console.log(res.data.Data.StockNumber)
        var stockNumber = res.data.Data.StockNumber;
        if (stockNumber > realCount) {
          if (t == 1) {
            var pindex = e.currentTarget.dataset.pindex;
            list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
          } else if (t == 2) {
            var pindex = e.currentTarget.dataset.pindex;
            list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
          }
          if (index !== "" && index != null) {
            if (list[parseInt(index)].OrderProductInfo.BuyCount > stockNumber) return;
            var selList = that.getSelList(cart);
            var buyCount = list[parseInt(index)].OrderProductInfo.BuyCount + 1;
            console.log(buyCount);
            wx.request({
              url: app.globalData.host + '/api/cart/ChangeProductCount',
              data: {
                pid: list[parseInt(index)].OrderProductInfo.Pid,
                buyCount: buyCount,
                uid: app.globalData.userInfo.Uid,
                selList: selList
              },
              success: function (res) {
                console.log(res);
                if (res.data.Code == 0) {
                  var cart = that.processImageHost(res.data.Data);
                  that.setData({ cartInfo: cart });
                  that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
                } else {
                  wx.showToast({
                    title: '系统错误',
                    content: res.data.Message
                  })
                }
              }
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '商品库存不足',
            showCancel: false
          })
          return
        }
      },
    })
  },
  jianBtnTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var t = e.currentTarget.dataset.type;//移动类型0：普通商品1：满减商品
    var cart = this.data.cartInfo;
    var list = cart.StoreCartList[0].CartProductList;
    if (t == 1) {
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullCutList[parseInt(pindex)].FullCutCartProductList;
    } else if (t == 2) {
      var pindex = e.currentTarget.dataset.pindex;
      list = cart.StoreCartList[0].CartFullSendList[parseInt(pindex)].FullSendMainCartProductList;
    } 
      
    if (index !== "" && index != null) {
      if (list[parseInt(index)].OrderProductInfo.BuyCount < 2)return;
      var selList = that.getSelList(cart);
      var buyCount = list[parseInt(index)].OrderProductInfo.BuyCount - 1;
      console.log(buyCount);
      wx.request({
        url: app.globalData.host + '/api/cart/ChangeProductCount',
        data: {
          pid: list[parseInt(index)].OrderProductInfo.Pid,
          buyCount: buyCount,
          uid: app.globalData.userInfo.Uid,
          selList: selList
        },
        success: function (res) {
          console.log(res);
          if (res.data.Code == 0) {
            var cart = that.processImageHost(res.data.Data);
            that.setData({ cartInfo: cart });
            that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
          } else {
            wx.showToast({
              title: '系统错误',
              content: res.data.Message
            })
          }
        }
      })
    }
  },
  editTap: function () {
    
    this.setGoodsList(!this.getSaveHide(), this.allSelect(), this.noSelect());
  },
  saveTap: function () {
    
    this.setGoodsList(!this.getSaveHide(), this.allSelect(), this.noSelect());
  },
  getSaveHide: function () {
    return this.data.saveHidden;
  },
  deleteSelected: function () {
    var that = this;
    var cart = this.data.cartInfo;
    var list = cart.StoreCartList[0].CartProductList;
    var cutList = cart.StoreCartList[0].CartFullCutList;
    var SendList = cart.StoreCartList[0].CartFullSendList;
    var pids='';
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      if (curItem.IsSelected) {
        pids+=curItem.OrderProductInfo.Pid+',';
        curItem.IsSelected=false;
      }
    }
    for (var i = 0; i < cutList.length; i++) {
      for (var j = 0; j < cutList[i].FullCutCartProductList.length; j++) {
        var curItem = cutList[i].FullCutCartProductList[j];
        if (curItem.IsSelected) {
          pids += curItem.OrderProductInfo.Pid + ',';
          curItem.IsSelected = false;
        }
      }
    }
    for (var i = 0; i < SendList.length; i++) {
      for (var j = 0; j < SendList[i].FullSendMainCartProductList.length; j++) {
        var curItem = SendList[i].FullSendMainCartProductList[j];
        if (curItem.IsSelected) {
          pids += curItem.OrderProductInfo.Pid + ',';
          curItem.IsSelected = false;
        }
      }
    }
    var selList = that.getSelList(cart);
    console.log(pids);
    wx.request({
      url: app.globalData.host + '/api/cart/DelProducts',
      data: {
        pids: pids,
        uid: app.globalData.userInfo.Uid,
        selList: selList
      },
      success: function (res) {
        console.log(res);
        if (res.data.Code == 0) {
          var cart = that.processImageHost(res.data.Data);
          that.setData({ cartInfo: cart });
          that.setGoodsList(that.getSaveHide(), that.allSelect(), that.noSelect());
        } else {
          wx.showToast({
            title: '系统错误',
            content: res.data.Message
          })
        }
      }
    })
    this.setGoodsList(this.getSaveHide(), this.allSelect(), this.noSelect());
  },
  getSelList: function (cart) {
    // console.log(cart);
    var selList = '';
    var list = cart.StoreCartList[0].CartProductList;
    var cutList = cart.StoreCartList[0].CartFullCutList;
    var suitList = this.data.cartInfo.StoreCartList[0].CartSuitList;
    var SendList = cart.StoreCartList[0].CartFullSendList;
    for (var i = 0; i < SendList.length; i++) {
      for (var j = 0; j < SendList[i].FullSendMainCartProductList.length; j++) {
        var curItem = SendList[i].FullSendMainCartProductList[j];
        if (curItem.IsSelected) {
          selList += curItem.OrderProductInfo.Type + '_' + curItem.OrderProductInfo.Pid + ','
        }
      }
    }
    for (var i = 0; i < list.length; i++) {
      if (list[i].IsSelected) {
        selList += list[i].OrderProductInfo.Type + '_' + list[i].OrderProductInfo.Pid + ','
      }

    }
    for (var i = 0; i < cutList.length; i++) {
      for (var j = 0; j < cutList[i].FullCutCartProductList.length; j++) {
        var curItem = cutList[i].FullCutCartProductList[j];
        if (curItem.IsSelected) {
          selList += curItem.OrderProductInfo.Type + '_' + curItem.OrderProductInfo.Pid + ','
        } 
      }
    }
    for (var i = 0; i < suitList.length; i++) {
      var curItem = suitList[i];
      if (curItem.IsSelected) {
        selList += 1 + '_' + curItem.PmId + ','
      }
    }
    if (selList == '') { selList = '_' };
    // console.log('selList:' + selList);
    return selList;
  },
  processImageHost: function (cart) {
    if (cart.StoreCartList.length > 0) {
      for (var i = 0; i < cart.StoreCartList[0].CartProductList.length; i++) {
        //主图
        cart.StoreCartList[0].CartProductList[i].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreCartList[0].CartProductList[i].OrderProductInfo.ShowImg;
        if (cart.StoreCartList[0].CartProductList[i].GiftList.length > 0) {
          for (var j = 0; j < cart.StoreCartList[0].CartProductList[i].GiftList.length; j++) {
            //赠品图片
            cart.StoreCartList[0].CartProductList[i].GiftList[j].ShowImg = app.globalData.image190_190host + cart.StoreCartList[0].CartProductList[i].GiftList[j].ShowImg;
          }
        }
      }
      for (var i = 0; i < cart.StoreCartList[0].CartFullCutList.length; i++) {
        for (var j = 0; j < cart.StoreCartList[0].CartFullCutList[i].FullCutCartProductList.length;j++){
          cart.StoreCartList[0].CartFullCutList[i].FullCutCartProductList[j].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreCartList[0].CartFullCutList[i].FullCutCartProductList[j].OrderProductInfo.ShowImg;
        }
      }
      for (var i = 0; i < cart.StoreCartList[0].CartSuitList.length; i++) {
        for (var j = 0; j < cart.StoreCartList[0].CartSuitList[i].CartProductList.length; j++) {
          cart.StoreCartList[0].CartSuitList[i].CartProductList[j].OrderProductInfo.ShowImg = app.globalData.image190_190host + cart.StoreCartList[0].CartSuitList[i].CartProductList[j].OrderProductInfo.ShowImg;
        }
      }
      
    }
    return cart;

  },
  toPayOrder: function () {
    var that = this;
    if (this.data.noSelect) {
      return;
    }
    console.log(that.data.cartInfo)
    var selList = that.getSelList(that.data.cartInfo);
    console.log(selList);
    wx.navigateTo({
      url: "/pages/to-pay-order/index?sl=" + selList
    })
      
  },

  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  }



})
